﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace bxml.betterxml.handlers
{

    /**
     * 
     * @author gkt, mbone
     * 
     * This handler class is responsible for turning SAX(2) events into BetterXML
     * content handler events.
     *
     */
    public class Sax2BetterXmlContentHandlerAdapter : ExpatLib
    {
        private BetterXmlContentHandler xirHandler;
        private Dictionary<string, string> UriMap = null;

        public Sax2BetterXmlContentHandlerAdapter(BetterXmlContentHandler xirHandler)
        {
            this.xirHandler = xirHandler;
            UriMap = new Dictionary<string, string>();
        }

        /*
         * Everything below here until the next comment are implementations of
         * SAX2 interface methods--all translate and call the appropriate XIRContentHandler methods
         */
        protected override void Characters(int length, string data)
        {
            xirHandler.characters(length, data);
        }

        protected void EndDocument() //throws SAXException 
        {
            xirHandler.endDocument();
        }

        protected override void EndElement(String uri, String localName, String qName)
        {
            xirHandler.endElement(uri, localName, qName);
        }

        protected override void EndPrefixMapping(String prefix) // throws SAXException 
        {
            xirHandler.endPrefixMapping(prefix);
        }

        //public void ignorableWhitespace(char[] ch, int start, int length) // throws SAXException 
        //{
        //    xirHandler.whitespace(length, new String(ch, start, length));
        //}

        protected override void ProcessingInstruction(String target, String data) // throws SAXException 
        {
            xirHandler.processingInstruction(data, target);
        }

        public void skippedEntity(String name) // throws SAXException 
        {
            xirHandler.skippedEntity(name);
        }

        public void StartDocument() //throws SAXException 
        {
            xirHandler.startDocument();
        }

        protected override void StartElement(String uri, string localName, string qName, string[] atts) //throws SAXException, XIRDataObjectException 
        {
            xirHandler.startElement(uri, localName, qName, atts.Length);

            for (int i = 0; i < atts.Length; i++)
            {
                //TODO what is this ternary operator doing, is it necessary?
                //String attrUri = atts.getURI(i) == "" ? uri : atts.getURI(i);
                //xirHandler.attribute(attrUri, atts.getLocalName(i), atts.getQName(i), atts.getValue(i));
            }
        }

        public override void StartPrefixMapping(String prefix, String uri) //throws SAXException 
        {
            xirHandler.startPrefixMapping(prefix, uri);
        }


        public void comment(char[] ch, int start, int length) //throws SAXException 
        {
            // TODO Auto-generated method stub
        }

        public void endCDATA() //throws SAXException 
        {
            // TODO Auto-generated method stub
        }

        public void endDTD() //throws SAXException 
        {
            // TODO Auto-generated method stub
        }

        public void endEntity(String name) //throws SAXException 
        {
            // TODO Auto-generated method stub
        }

        public void startCDATA() //throws SAXException 
        {
            // TODO Auto-generated method stub
        }

        public void startDTD(String name, String publicId, String systemId) //throws SAXException 
        {
            // TODO Auto-generated method stub
        }

        public void startEntity(String name) //throws SAXException 
        {
            // TODO Auto-generated method stub
        }

    }
}
